package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Playlist;
import com.softeem.music_sys.service.PlaylistService;
import com.softeem.music_sys.mapper.PlaylistMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author RD
* @description 针对表【playlist(歌单信息表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class PlaylistServiceImpl extends ServiceImpl<PlaylistMapper, Playlist>
    implements PlaylistService{

    @Override
    public Result addPlaylist(Playlist playlist) {
        // 设置创建时间
        playlist.setCreateTime(new Date());
        
        boolean result = this.save(playlist);
        return result ? Result.success().setMessage("创建成功") : Result.fail("创建失败");
    }

    @Override
    public Result getPlaylistById(Integer id) {
        Playlist playlist = this.getById(id);
        if (playlist == null) {
            return Result.fail("歌单不存在");
        }
        return Result.success().setData(playlist);
    }

    @Override
    public Result updatePlaylist(Playlist playlist) {
        Playlist existPlaylist = this.getById(playlist.getId());
        if (existPlaylist == null) {
            return Result.fail("歌单不存在");
        }
        boolean result = this.updateById(playlist);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deletePlaylist(Integer id) {
        Playlist playlist = this.getById(id);
        if (playlist == null) {
            return Result.fail("歌单不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Page<Playlist> getPlaylistList(Integer page, Integer limit) {
        Page<Playlist> pageUtil = new Page<>(page, limit);
        return page(pageUtil);
    }

    @Override
    public Page<Playlist> getPlaylistsByUser(Integer userId, Integer page, Integer limit) {
        Page<Playlist> pageUtil = new Page<>(page, limit);
        QueryWrapper<Playlist> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("create_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Playlist> searchPlaylists(String keyword, Integer page, Integer limit) {
        Page<Playlist> pageUtil = new Page<>(page, limit);
        QueryWrapper<Playlist> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword);
        wrapper.orderByDesc("create_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Playlist> getNewPlaylists(Integer page, Integer limit) {
        Page<Playlist> pageUtil = new Page<>(page, limit);
        QueryWrapper<Playlist> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return page(pageUtil, wrapper);
    }
}